package com.fortune.funcjar;

import java.io.IOException;
import java.io.InputStream;

public class FileUtils {

    public static byte[] getFileByte(InputStream in) throws IOException {
        try {
            int fileSize = in.available();
            if (fileSize > Integer.MAX_VALUE) {
                System.out.println("file too big...");
                return null;
            }

            byte[] buffer = new byte[(int) fileSize];
            int offset = 0;
            int numRead = 0;
            while (offset < buffer.length
                    && (numRead = in.read(buffer, offset, buffer.length - offset)) >= 0) {
                offset += numRead;
            }

            if (offset != buffer.length) {
                throw new IOException("Could not completely read file ");
            }
            in.close();

            return buffer;
        } catch (Exception e) {
            throw e;
        }
    }
}
